﻿using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLine.Models.PackageModel;

namespace ZoneLine.ViewModels
{
    public class WorkSituationViewModel : ObservableObject
    {
        private IModelDAL _dal;

        public WorkSituationViewModel(IModelDAL modelDAL)
        {

            _dal = modelDAL;

        }

        private string _absScanCode;

        public string AbsScanCode
		{
			get { return _absScanCode; }
			set { _absScanCode = value; base.OnPropertyChanged(); }
		}


		private ObservableCollection<WorkSituationModel> _workSituationList;

		public ObservableCollection<WorkSituationModel> WorkSituationList
        {
			get { return _workSituationList; }
			set { _workSituationList = value; base.OnPropertyChanged(); }
		}


        #region Commands

        public RelayCommand SelectQueryCmd => new RelayCommand(() =>
        {
            if (this.AbsScanCode != "")
            {
                var list = _dal.ExceSqlToList<WorkSituationModel>("select * from v_checkstationworkinfo where AbsScanCode like '%" + AbsScanCode + "%'");
                if (list != null && list.Count > 0)
                {
                    WorkSituationList = new ObservableCollection<WorkSituationModel>(list);
                }
                else {
                    WorkSituationList = new ObservableCollection<WorkSituationModel>();
                }
            }
            else
            {
                HandyControl.Controls.MessageBox.Error("缸体码信息为空！", "温馨提示");
            }
        });


        public RelayCommand<WorkSituationModel> DeleteCmd => new RelayCommand<WorkSituationModel>((obj) =>
        {
            if (obj != null)
            {
                var res = HandyControl.Controls.MessageBox.Ask("此操作不可逆，是否确定删除当前工站的工作信息？", "温馨提示");
                if (res == MessageBoxResult.OK)
                {
                    var rlt = _dal.ExceSql($"delete from {obj.TableName} where AbsScanCode = '{obj.AbsScanCode}'");
                    if (rlt)
                    {
                        Logger.Log.Info($"删除{obj.AbsScanCode},{obj.Station}的数据删除成功！");
                        HandyControl.Controls.MessageBox.Success("删除成功！", "温馨提示");
                        SelectQueryCmd.Execute(null);
                    }
                }
            }
        });

        #endregion



    }


    [FreeSql.DataAnnotations.Table(Name = "v_checkstationworkinfo")]
    public class WorkSituationModel {
        //AbsScanCode	Station	WorkSituation	WorkTime	WorkResult	TableName
        public string AbsScanCode { get; set; }
        public string Station { get; set; }
        public string WorkSituation { get; set; }
        public DateTime WorkTime { get; set; }
        public string WorkResult { get; set; }
        public string TableName { get; set; }
    }
}
